home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / contrib / kostas / tex-support.el (.txt) < prev    next >
LaTeX Document  |  1995-02-24  |  15KB  |  324 lines

  1. ;; Kostas Oikonomou, September 1993.
  2. ;; Removed the support for plain TeX, which didn't work anyway.
  3. ;; Added a new function make-file, based on tex-file. This function is used by noweb-mode.
  4. ;; Kostas Oikonomou, December 1992.
  5. ;; This is hacked from tex-mode.el. A lot of code has been removed.
  6. ;; Besides that, the most notable modifications are that the functions latex-mode
  7. ;; and tex-mode (which conflict with the functions in Beebe's latex.el) have been
  8. ;; renamed setup-latex and setup-tex. These functions must be called by LaTeX-mode-hook 
  9. ;; and TeX-mode-hook for things to work properly.
  10. ;; Other modifications:
  11. ;;  1) A line has been added to the function tex-file.
  12. ;;  2) The "-v" has been deleted from the make-comint line.
  13. ;;  3) Two lines have been added near the end of tex-file to keep the output
  14. ;;     in *tex-shell* visible. Same was done to tex-region.
  15. ;;  4) The function tex-delete-temp-files has been made interactive.
  16. ;; Still to do:
  17. ;;  a) Use re-search for tex-start-of-header, etc., so both "\begin{document}" and 
  18. ;;     "\begin {document}" can be used.
  19. (require 'comint)
  20. (provide 'tex-support)
  21. (defvar tex-shell-file-name nil
  22.   "*If non-nil, is file name to use for the subshell in which TeX is run.")
  23. (defvar tex-directory "."
  24.   "*Directory in which temporary files are left.
  25. You can make this /tmp if your TEXINPUTS has no relative directories in it
  26. and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
  27. \\input commands with relative directories.")
  28. (defvar latex-run-command "latex"
  29.   "*Command used to run LaTeX subjob.
  30. If this string contains an asterisk (*), it will be replaced by the
  31. filename; if not, the name of the file, preceded by blank, will be added to
  32. this string.")
  33. (defvar tex-dvi-print-command "lpr -d"
  34.   "*Command used by \\[tex-print] to print a .dvi file.
  35. If this string contains an asterisk (*), it will be replaced by the
  36. filename; if not, the name of the file, preceded by blank, will be added to
  37. this string.")
  38. (defvar tex-alt-dvi-print-command "lpr -d"
  39.   "*Command used by \\[tex-print] with a prefix arg to print a .dvi file.
  40. If this string contains an asterisk (*), it will be replaced by the
  41. filename; if not, the name of the file, preceded by blank, will be added to
  42. this string.
  43. If two printers are not enough of a choice, you can define the value
  44. of tex-alt-dvi-print-command to be an expression that asks what you want;
  45. for example,
  46.     (setq tex-alt-dvi-print-command
  47.          '(format \"lpr -P%s\" (read-string \"Use printer: \")))
  48. would tell \\[tex-print] with a prefix argument to ask you which printer to
  49. use.")
  50. (defvar tex-dvi-view-command nil
  51.   "*Command used by \\[tex-view] to display a .dvi file.
  52. If this string contains an asterisk (*), it will be replaced by the
  53. filename; if not, the name of the file, preceded by blank, will be added to
  54. this string.
  55. This can be set conditionally so that the previewer used is suitable for the
  56. window system being used.  For example,
  57.     (setq tex-dvi-view-command
  58.           (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\"))
  59. would tell \\[tex-view] use xdvi under X windows and to use dvi2tty
  60. otherwise.")
  61. (defvar tex-last-temp-file nil
  62.   "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
  63. Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the
  64. tex-shell goes away.")
  65. (defvar tex-command nil
  66.   "Command to run TeX.
  67. The name of the file, preceded by a blank, will be added to this string.")
  68. (defvar tex-trailer nil
  69.   "String appended after the end of a region sent to TeX by \\[tex-region].")
  70. (defvar tex-start-of-header nil
  71.   "String used by \\[tex-region] to delimit the start of the file's header.")
  72. (defvar tex-end-of-header nil
  73.   "String used by \\[tex-region] to delimit the end of the file's header.")
  74. (defvar tex-shell-cd-command "cd"
  75.   "Command to give to shell running TeX to change directory.
  76. The value of tex-directory will be appended to this, separated by a space.")
  77. (defvar tex-zap-file nil
  78.   "Temporary file name used for text being sent as input to TeX.
  79. Should be a simple file name with no extension or directory specification.")
  80. (defvar tex-last-buffer-texed nil
  81.   "Buffer which was last TeXed.")
  82. (defvar tex-print-file nil
  83.   "File name that \\[tex-print] prints.
  84. Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
  85. (defvar tex-shell-map nil
  86.   "Keymap for the tex-shell.  A comint-mode-map with a few additions.")
  87. (defun setup-latex ()
  88.   "Give various variables values appropriate for LaTeX mode"
  89.    (interactive)
  90.    (tex-common-initialization)
  91.    (setq tex-command latex-run-command)
  92.    (setq tex-start-of-header "\\documentstyle")
  93.    (setq tex-end-of-header "\\begin {document}")
  94.    (setq tex-trailer "\\end {document}\n")
  95. (defun tex-common-initialization ()
  96.   (make-local-variable 'tex-command)
  97.   (make-local-variable 'tex-start-of-header)
  98.   (make-local-variable 'tex-end-of-header)
  99.   (make-local-variable 'tex-trailer))
  100. ;;; Invoking TeX in an inferior shell.
  101. ;;; Why use a shell instead of running TeX directly?  Because if TeX
  102. ;;; gets stuck, the user can switch to the shell window and type at it.
  103. ;;; The utility functions:
  104. (defun tex-start-shell ()
  105.   (save-excursion
  106.     (set-buffer
  107.      (make-comint
  108.       "tex-shell"
  109.       (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh")))
  110.     (let ((proc (get-process "tex-shell")))
  111.       (set-process-sentinel proc 'tex-shell-sentinel)
  112.       (process-kill-without-query proc)
  113.       (setq tex-shell-map (copy-keymap comint-mode-map))
  114.       (while (zerop (buffer-size))
  115.           (sleep-for 1)))))
  116. (defun tex-shell-sentinel (proc msg)
  117.   (cond ((null (buffer-name (process-buffer proc)))
  118.      ;; buffer killed
  119.      (set-process-buffer proc nil)
  120.          (tex-delete-last-temp-files))
  121.     ((memq (process-status proc) '(signal exit))
  122.          (tex-delete-last-temp-files))))
  123. (defun tex-set-buffer-directory (buffer directory)
  124.   "Set BUFFER's default directory to be DIRECTORY."
  125.   (setq directory (file-name-as-directory (expand-file-name directory)))
  126.   (if (not (file-directory-p directory))
  127.       (error "%s is not a directory" directory)
  128.     (save-excursion
  129.       (set-buffer buffer)
  130.       (setq default-directory directory))))
  131. (defun tex-send-command (command &optional file background)
  132.   "Send COMMAND to tex-shell, substituting optional FILE for *; in background
  133. if optional BACKGROUND is t.   If COMMAND has no *, FILE will be appended,
  134. preceded by a blank, to COMMAND.  If FILE is nil, no substitution will be made
  135. in COMMAND.  COMMAND can be any expression that evaluates to a command string."
  136.   (save-excursion
  137.     (let* ((cmd (eval command))
  138.            (star (string-match "\\*" cmd)))
  139.       (comint-proc-query (get-process "tex-shell")
  140.                          (concat (substring cmd 0 star)
  141.                                  (if file (concat " " file) "")
  142.                                  (if star (substring cmd (1+ star) nil) "")
  143.                                  (if background "&\n" "\n"))))))
  144. (defun tex-delete-last-temp-files ()
  145.   "Delete any junk files from last temp file."
  146.   (interactive)
  147.   (if tex-last-temp-file
  148.       (let* ((dir (file-name-directory tex-last-temp-file))
  149.              (list (file-name-all-completions
  150.                     (file-name-nondirectory tex-last-temp-file) dir)))
  151.         (while list
  152.           (delete-file (concat dir (car list)))
  153.           (setq list (cdr list))))))
  154. (setq kill-emacs-hook 'tex-delete-last-temp-files)
  155. ;;; The commands:
  156. (defun tex-region (beg end)
  157.   "Run TeX on the current region, via a temporary file.
  158. The file's name comes from the variable `tex-zap-file' and the
  159. variable `tex-directory' says where to put it.
  160. If the buffer has a header, the header is given to TeX before the
  161. region itself.  The buffer's header is all lines between the strings
  162. defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
  163. The header must start in the first 100 lines of the buffer.
  164. The value of `tex-trailer' is given to TeX as input after the region.
  165. The value of `tex-command' specifies the command to use to run TeX."
  166.   (interactive "r")
  167.   (if (tex-shell-running)
  168.       (tex-kill-job)
  169.     (tex-start-shell))
  170.   (or tex-zap-file
  171.       (setq tex-zap-file (tex-generate-zap-file-name)))
  172.   (let* ((temp-buffer (get-buffer-create " TeX-Output-Buffer"))
  173.          ; Temp file will be written and TeX will be run in zap-directory.
  174.          ; If the TEXINPUTS file has relative directories or if the region has
  175.          ; \input of files, this must be the same directory as the file for
  176.          ; TeX to access the correct inputs.  That's why it's safest if
  177.          ; tex-directory is ".".
  178.          (zap-directory
  179.           (file-name-as-directory (expand-file-name tex-directory)))
  180.          (tex-out-file (concat zap-directory tex-zap-file)))
  181.     (tex-delete-last-temp-files)
  182.     ;; Write the new temp file.
  183.     (save-excursion
  184.       (save-restriction
  185.     (widen)
  186.     (goto-char (point-min))
  187.     (forward-line 100)
  188.     (let ((search-end (point))
  189.           (hbeg (point-min)) (hend (point-min))
  190.           (default-directory zap-directory))
  191.       (goto-char (point-min))
  192.       ;; Initialize the temp file with either the header or nothing
  193.       (if (search-forward tex-start-of-header search-end t)
  194.           (progn
  195.         (beginning-of-line)
  196.         (setq hbeg (point))    ;mark beginning of header
  197.         (if (search-forward tex-end-of-header nil t)
  198.             (progn (forward-line 1)
  199.                (setq hend (point)))    ;mark end of header
  200.           (setq hbeg (point-min))))) ;no header
  201.       (write-region (min hbeg beg) hend
  202.                         (concat tex-out-file ".tex") nil nil)
  203.       (write-region (max beg hend) end (concat tex-out-file ".tex") t nil))
  204.     (let ((local-tex-trailer tex-trailer))
  205.       (set-buffer temp-buffer)
  206.       (erase-buffer)
  207.       ;; make sure trailer isn't hidden by a comment
  208.       (insert "\n")
  209.       (if local-tex-trailer (insert local-tex-trailer))
  210.       (tex-set-buffer-directory temp-buffer zap-directory)
  211.       (write-region (point-min) (point-max)
  212.                         (concat tex-out-file ".tex") t nil))))
  213.     ;; Record the file name to be deleted afterward.
  214.     (setq tex-last-temp-file tex-out-file)
  215.     (tex-send-command tex-shell-cd-command zap-directory)
  216.     (tex-send-command tex-command tex-out-file)
  217.     (pop-to-buffer "*tex-shell*") ; Added by K.O.
  218.     (goto-char (point-max)) ; Added by K.O.
  219.     (setq tex-print-file tex-out-file)
  220.     (setq tex-last-buffer-texed (current-buffer))))
  221. (defun tex-buffer ()
  222.   "Run TeX on current buffer.  See \\[tex-region] for more information.
  223. Does not save the buffer, so it's useful for trying experimental versions.
  224. See \\[tex-file] for an alternative."
  225.   (interactive)
  226.   (tex-region (point-min) (point-max)))
  227. (defun tex-file ()
  228.   "Run TeX (or LaTeX) on current buffer's file.
  229. This function is more useful than \\[tex-buffer] when you need the
  230. `.aux' file of LaTeX to have the correct name."
  231.   (interactive)
  232.   (let ((tex-out-file
  233.          (if (buffer-file-name)
  234.              (file-name-nondirectory (buffer-file-name))
  235.            (error "Buffer does not seem to be associated with any file")))
  236.     (file-dir (file-name-directory (buffer-file-name))))
  237.     (basic-save-buffer) ; Added by K.O.
  238.     (if (tex-shell-running)
  239.     (tex-kill-job)
  240.       (tex-start-shell))
  241.     (tex-send-command tex-shell-cd-command file-dir)
  242.     (tex-send-command tex-command tex-out-file)
  243.     (pop-to-buffer "*tex-shell*") ; Added by K.O.
  244.     (goto-char (point-max)) ; Added by K.O.
  245.   (setq tex-last-buffer-texed (current-buffer))
  246.   (setq tex-print-file (buffer-file-name))
  247. (defun make-file ()
  248.   "Modification of tex-file, by leaving out the tex-out-file from tex-send-command"
  249.   (interactive)
  250.     (basic-save-buffer) ; Added by K.O.
  251.     (if (tex-shell-running)
  252.     (tex-kill-job)
  253.       (tex-start-shell))
  254.     (tex-send-command tex-shell-cd-command (file-name-directory (buffer-file-name)))
  255.     (tex-send-command tex-command)
  256.     (pop-to-buffer "*tex-shell*") ; Added by K.O.
  257.     (goto-char (point-max)) ; Added by K.O.
  258.   (setq tex-last-buffer-texed (current-buffer))
  259.   (setq tex-print-file (buffer-file-name))
  260. (defun tex-generate-zap-file-name ()
  261.   "Generate a unique name suitable for use as a file name."
  262.   ;; Include the shell process number and host name
  263.   ;; in case there are multiple shells (for same or different user).
  264.   (format "#tz%d%s"
  265.           (process-id (get-buffer-process "*tex-shell*"))
  266.       (tex-strip-dots (system-name))))
  267. (defun tex-strip-dots (s)
  268.   (setq s (copy-sequence s))
  269.   (while (string-match "\\." s)
  270.     (aset s (match-beginning 0) ?-))
  271. ;; This will perhaps be useful for modifying TEXINPUTS.
  272. ;; Expand each file name, separated by colons, in the string S.
  273. (defun tex-expand-files (s)
  274.   (let (elts (start 0))
  275.     (while (string-match ":" s start)
  276.       (setq elts (cons (substring s start (match-beginning 0)) elts))
  277.       (setq start (match-end 0)))
  278.     (or (= start 0)
  279.     (setq elts (cons (substring s start) elts)))
  280.     (mapconcat 'expand-file-name (nreverse elts) ":")))
  281. (defun tex-shell-running ()
  282.   (and (get-process "tex-shell")
  283.        (eq (process-status (get-process "tex-shell")) 'run)))
  284. (defun tex-kill-job ()
  285.   "Kill the currently running TeX job."
  286.   (interactive)
  287.   (quit-process (get-process "tex-shell") t))
  288. (defun tex-print (&optional alt)
  289.   "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
  290. Runs the shell command defined by tex-dvi-print-command.  If prefix argument
  291. is provided, use the alternative command, tex-alt-dvi-print-command."
  292.   (interactive "P")
  293.   (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
  294.     test-name)
  295.     (if (and (not (equal (current-buffer) tex-last-buffer-texed))
  296.          (file-newer-than-file-p
  297.           (setq test-name (tex-append (buffer-file-name) ".dvi"))
  298.           print-file-name-dvi))
  299.     (setq print-file-name-dvi test-name))
  300.     (if (not (file-exists-p print-file-name-dvi))
  301.         (error "No appropriate `.dvi' file could be found")
  302.       (tex-send-command
  303.         (if alt tex-alt-dvi-print-command tex-dvi-print-command)
  304.         print-file-name-dvi t))))
  305. (defun tex-view ()
  306.   "Preview the last `.dvi' file made by running TeX under Emacs.
  307. This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
  308. The variable `tex-dvi-view-command' specifies the shell command for preview."
  309.   (interactive)
  310.   (let ((tex-dvi-print-command tex-dvi-view-command))
  311.     (tex-print)))
  312. (defun tex-append (file-name suffix)
  313.   "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
  314. Scans for the first (not last) period.
  315. No period is retained immediately before SUFFIX,
  316. so normally SUFFIX starts with one."
  317.   (if (stringp file-name)
  318.       (let ((file (file-name-nondirectory file-name)))
  319.     (concat (file-name-directory file-name)
  320.         (substring file 0
  321.                (string-match "\\." file))
  322.         suffix))
  323.     " "))
  324.